import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

import axios from "@/api/request";

// 用redux的话 处理异步得下载一个redux-thunk 去处理异步
// 如果是 redux toolkit 内就有一个createAsyncThunk去处理异步

// 定义一下 initialState  的类型
export interface CounterState {
  cardSingle: any;
}

const initialState: CounterState = {
  cardSingle: [],
};

export const getcardSingle = createAsyncThunk("getcardSingle", async () => {
  const res = await axios.get("/get/daytui");
  console.log("res", res);
  return res.data;
});

const getCardSingle = createSlice({
  name: "card",
  initialState,
  reducers: {},
  extraReducers(builder) {
    builder.addCase(getcardSingle.fulfilled, (state, action) => {
      state.cardSingle.push(action.payload);
    });
  },
});

// 提供给useSelector使用，用于获取cards列表
export const selectSinglecards = (state: any) => {
  state.cardSingleReducer.cardSingle;
};

export default getCardSingle.reducer;
